<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>在线考试平台</title>
    <link rel="shortcut icon" href="static/layui/images/exam.png" type="image/x-icon">
    <link rel="stylesheet" href="static/layui/css/font.css">
    <link rel="stylesheet" href="static/layui/css/xadmin.css">
    <script src="static/layui/lib/layui/layui.js"></script>
    <script src="static/layui/js/xadmin.js"></script>
    <script src="static/layui/js/jquery.min.js"></script>

</head>

<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15" style="background: #f7f8fc">
        <div class="layui-col-sm12 layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <button class="layui-btn" onclick="add_class()"><i class="layui-icon layui-icon-add-1"></i>添加
                    </button>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-row layui-col-space15" style="background: #f7f8fc" id="class-list">

    </div>
</div>

</body>

<script>
    layui.use(['element', "form", 'jquery', 'layer'], function () {
        const form = layui.form;
        const $ = layui.jquery;
        const layer = layui.layer;
        const element = layui.element;
        get_all_class()
    });

    function get_all_class() {
        $.ajax({
            url: 'get_all_class',
            dataType: "json",//数据格式
            type: "post",//请求方式
            async: false,//是否异步请求
            success: function (data) {
                if (data.code === 200) {
                    var text = ""
                    for (let i = 0; i < data.result.length; i++) {
                        text += "<div class=\"layui-col-sm12 layui-col-md2\">" +
                            "            <div class=\"layui-card\">" +
                            "                <div class=\"layui-card-header\">" +
                            "                    <span style=\"float: left\">" + getDateTimeStamp(data.result[i].c_creat_time) + "</span>" +
                            "                    <span style=\"float: right\"><a href=\"#\" onclick='del_class_by_c_id(" + data.result[i].c_id + ")'><i class=\"layui-icon layui-icon-delete\"></i>删除</a></span>" +
                            "                </div>" +
                            "                <a href=\"#\" onclick=\"get_class_user_by_c_id('" + data.result[i].c_id + "','" + data.result[i].c_name + "')\"><div class=\"layui-card-body\" style=\"text-align: center;height: 50px\">" +
                            "                    <span style=\"font-size: 18px\">" + data.result[i].c_name + "</span>" +
                            "                </div>" +
                            "                </a>" +
                            "            </div>" +
                            "        </div>"
                    }
                    $("#class-list").html(text)
                }
            },
            error: function (e) {
                alert("出现错误！！");
            }
        });
    }

    function getDateDiff(dateTimeStamp) {
        var minute = 1000 * 60;
        var hour = minute * 60;
        var day = hour * 24;
        var halfamonth = day * 15;
        var month = day * 30;
        var now = new Date().getTime();
        var diffValue = now - dateTimeStamp;
        if (diffValue < 0) {
            //若日期不符则弹出窗口告之
            //alert("结束日期不能小于开始日期！");
        }
        var monthC = diffValue / month;
        var weekC = diffValue / (7 * day);
        var dayC = diffValue / day;
        var hourC = diffValue / hour;
        var minC = diffValue / minute;
        if (monthC >= 1) {
            result = "创建于" + parseInt(monthC) + "个月前";
        } else if (weekC >= 1) {
            result = "创建于" + parseInt(weekC) + "周前";
        } else if (dayC >= 1) {
            result = "创建于" + parseInt(dayC) + "天前";
        } else if (hourC >= 1) {
            result = "创建于" + parseInt(hourC) + "个小时前";
        } else if (minC >= 1) {
            result = "创建于" + parseInt(minC) + "分钟前";
        } else
            result = "刚刚创建";
        return result;
    }

    //js函数代码：字符串转换为时间戳
    function getDateTimeStamp(dateStr) {
        var time_stamp = Date.parse(dateStr.replace(/-/gi, "/"));
        return getDateDiff(time_stamp)
    }

    function del_class_by_c_id(c_id) {
        layer.confirm('删除班级?会连同班级学生一起删除', {icon: 5}, function () {
            $.ajax({
                url: 'del_class_by_c_id',
                dataType: 'json',//数据格式
                type: 'post',//请求方式
                async: false,//是否异步请求
                data: {c_id: c_id},
                success: function (data) {
                    if (data.code === 200) {
                        layer.msg(data.message, {icon: 6, time: 1000}, function () {
                            get_all_class();
                        });
                    } else {
                        layer.msg(data.message, {icon: 5, time: 1000})
                    }
                },
                error: function (e) {
                    alert("出现错误！！");
                }
            });
        })
    }

    function add_class() {
        var text = "<div class=\"layui-form-item\">" +
            "                        <div class=\"layui-inline-block\">" +
            "                            <input type='text' placeholder=\"请输入班级名(最多10字)\" style=\"height: 100px;font-size:18px\" " +
            "                                   maxlength=\"10\"  class=\"layui-input\" lay-verify=\"required\"  id=\"class_content\">" +
            "                    </div>" +
            "       </div>"

        let index = layer.open({
            type: 1,
            title: '添加科目',
            closeBtn: 0,
            shadeClose: true,
            btn: '提交',
            yes: function (index, layero) {
                let class_content = $("#class_content").val();
                if (class_content.length === 0) {
                    layer.msg("内容不能为空!", {icon: 5})
                    return false
                } else {
                    $.ajax({
                        url: 'add_class',
                        dataType: "json",//数据格式
                        type: "post",//请求方式
                        async: false,//是否异步请求
                        data: {c_name: class_content},
                        success: function (data) {
                            if (data.code === 200) {
                                layer.msg(data.message, {icon: 6, time: 1000}, function () {
                                    get_all_class()
                                    layer.close(index)
                                })
                            } else {
                                layer.msg(data.message, {icon: 5, time: 1000})
                            }
                        },
                        error: function (e) {
                            alert("出现错误！！");
                        }
                    });
                }

            },
            skin: 'layui-layer-molv',
            area: ['400px', '250px'],
            content: text
        })
    }

    function get_class_user_by_c_id(c_id, c_name) {
        parent.xadmin.add_tab(c_name, 'get_class_user_by_c_id?c_id=' + c_id);
    }
</script>

</html>